package com.gams.springboot.services.impl;

import com.gams.springboot.dao.MenuDao;
import com.gams.springboot.dao.RoleDao;
import com.gams.springboot.pojo.Menu;
import com.gams.springboot.pojo.Role;
import com.gams.springboot.pojo.TreeMenu;
import com.gams.springboot.pojo.TreeMenu1;
import com.gams.springboot.services.MenuService;
import com.gams.springboot.services.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author tql
 * @current springboot
 * @create 2021-09-9-10:14
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> selectAllMenu() {
        return menuDao.selectAllMenu();
    }

    @Override
    public List<Menu> selectParentId(long parentId) {
        return menuDao.selectParentId(parentId);
    }


   @Override
    public List<TreeMenu> getTree(long id) {
        List<Menu> treeList = menuDao.selectParentId(id);
        List<TreeMenu> treeMenuList = new ArrayList<>();
        for (Menu m : treeList) {
            TreeMenu tree = new TreeMenu();
            tree.setTitle(m.getName());
            tree.setId(m.getId());
            tree.setIcon(m.getImgUrl());
            tree.setHref(m.getAppUrl());
            tree.setChildren(getTree(m.getId()));
            treeMenuList.add(tree);
        }
        return treeMenuList;
    }


    @Override
    public List<TreeMenu1> getTree1(long id) {
        List<Menu> treeList = menuDao.selectParentId(id);
        List<TreeMenu1> treeMenuList = new ArrayList<>();
        for (Menu m : treeList) {
            if (m.getIsMenu()==1){
                continue;
            }
            TreeMenu1 tree = new TreeMenu1();
                tree.setTitle(m.getName());
                tree.setId(m.getId());
                tree.setIcon(m.getImgUrl());
                tree.setHref(m.getAppUrl());
                tree.setChild(getTree1(m.getId()));
            treeMenuList.add(tree);
        }
        return treeMenuList;
    }




    @Override
    public int addMenu(Menu menu) {
        if(menu.getCreateTime()==null){
            menu.setCreateTime(new Date());
        }
        return menuDao.addMenu(menu);
    }

    @Override
    public int updateMenu(Menu menu) {
        if(menu.getUpdateTime()==null){
            menu.setCreateTime(new Date());
        }
        return menuDao.updateMenu(menu);
    }

    @Override
    public int deleteMenu(long id) {
        return menuDao.deleteMenu(id);
    }

    @Override
    public List<Menu> selectAllMenuByRoleId(long roleId) {
        return menuDao.selectAllMenuByRoleId(roleId);
    }

    @Override
    public List<Menu> selectAllMenuByUserId(long userId) {
        return menuDao.selectAllMenuByUserId(userId);
    }


}
